#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        int count = 0;
        int cur = -1;
        while (count < arr.size())
        {
            ++cur;

            if (arr[cur])
                count++;
            else
                count += 2;
        }

        int des = count - 1;

        while (cur >= 0 && des >= 0)
        {
            if (arr[cur])
            {
                if (des < arr.size())
                    arr[des--] = arr[cur];
                else
                    des--;
            }
            else
            {
                if (des < arr.size())
                    arr[des--] = 0;
                else
                    des--;

                if (des > 0)
                    arr[des--] = 0;
                else
                    break;
            }

            --cur;
        }
    }
};